$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'goodsCategoryScholarship/goodscategoryscholarship/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '分类ID', name: 'categoryId', index: 'category_id', width: 80,hidden:false },
            { label: '分类名', name: 'categoryName', index: 'category_name', width: 80 },
			{ label: '奖励金比例', name: 'deductionScholarship', index: 'deduction_scholarship', width: 80 }, 			
			{ label: '是否删除（true：是；false：否）', name: 'deleted', index: 'deleted', width: 80 }, 			
			{ label: '操作人姓名', name: 'createName', index: 'create_name', width: 80 }, 			
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            // root: "page.list",
            // page: "page.currPage",
            // total: "page.totalPage",
            // records: "page.totalCount"
            root: "page.records",
            page: "page.current",
            total: "page.pages",
            records: "page.total"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "categoryId",
            pIdKey: "parentCategoryId",
            rootPId: -1
        },
        key: {
            name: "categoryName",
            url:"nourl"
        }
    },
    height: 800,
};
var ztree;

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		goodsCategoryScholarship: {categoryName:null}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.goodsCategoryScholarship = {categoryName:null};
            vm.getGoodsCategory();
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			if(vm.validate()){
				return
			}
			var url = vm.goodsCategoryScholarship.id == null ? "goodsCategoryScholarship/goodscategoryscholarship/save" : "goodsCategoryScholarship/goodscategoryscholarship/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.goodsCategoryScholarship),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "goodsCategoryScholarship/goodscategoryscholarship/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "goodsCategoryScholarship/goodscategoryscholarship/info/"+id, function(r){
                vm.goodsCategoryScholarship = r.goodsCategoryScholarship;
                vm.getGoodsCategory();
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
        getGoodsCategory: function(){
            //加载商品分类树
            $.get(baseURL + "goodsCategory/goodscategory/select", function(r){
                ztree = $.fn.zTree.init($("#goodsCategoryTree"), setting, r.goodsCategoryEntityList);
                var node = ztree.getNodeByParam("categoryId", vm.goodsCategoryScholarship.categoryId);
                if(!isBlank(node)){
                    ztree.selectNode(node);
                    vm.goodsCategoryScholarship.categoryName  = node.categoryName;
				}
            })
        },
        goodsCategoryTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择商品分类",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#goodsCategoryLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    var mark = false;
                    $.get(baseURL + "goodsCategoryScholarship/goodscategoryscholarship/getScholarShipByCatId/"+node[0].categoryId, function(r){
                    	if(r.code == 0){
                            mark = r.mark;
                            if(mark){
                                //选择上级菜单
                                vm.goodsCategoryScholarship.categoryId  = node[0].categoryId;
                                vm.goodsCategoryScholarship.categoryName  = node[0].categoryName;
                            }else{
                                alert("该分类下已添加了奖励金设置,请去编辑")
                            }
                            layer.close(index);
                            mark = r.mark;
						}else{
                    		alert(r.msg);
                            layer.close(index);
						}
                    });

                }
            });
        },
        validate:function(){
			if(isBlank(vm.goodsCategoryScholarship.categoryId)){
				alert("请选择分类");
				return true;
			}
            if(isBlank(vm.goodsCategoryScholarship.deductionScholarship)){
                alert("请填些奖励金比例");
                return true;
            }
            if(vm.goodsCategoryScholarship.deductionScholarship > 1){
                alert("奖励金比例不能大于1");
                return true;
			}
			if(vm.goodsCategoryScholarship.deductionScholarship < 0){
                alert("奖励金比例不能小于0");
                return true;
            }
            return false;
        }
	}
});